﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace TradeMo
{
    public partial class SellingView 
    {
        private SellingViewModel _viewModel;
        public SellingView()
        {
            InitializeComponent();
            _viewModel = new SellingViewModel();
            DataContext = _viewModel;
            AnimationContext = LayoutRoot;
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            _viewModel.LoadSelling();
            _viewModel.LoadSold();
            _viewModel.LoadUnsold();

        }

        private void SellingListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (SellingListBox.SelectedItem == null) return;
            var selected = SellingListBox.SelectedItem as SearchResultItem;
            SellingListBox.SelectedItem = null;
            if (selected == null) return;

            OpenListing(selected.Listing.ListingId);
        }

        private void SoldListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (SoldListBox.SelectedItem == null) return;
            var selected = SoldListBox.SelectedItem as SearchResultItem;
            SoldListBox.SelectedItem = null;
            if (selected == null) return;

            OpenListing(selected.Listing.ListingId);
        }

        private void UnsoldListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (UnsoldListBox.SelectedItem == null) return;
            var selected = UnsoldListBox.SelectedItem as SearchResultItem;
            UnsoldListBox.SelectedItem = null;
            if (selected == null) return;

            OpenListing(selected.Listing.ListingId);

        }

        private void OpenListing(int listingId)
        {
            NavigationService.Navigate(new Uri("/ListingView.xaml?ListingId=" + listingId, UriKind.Relative));
        }

    }
}